import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
public class TestAPI4JavaDemoCaller {

    public static void main(String[] args) {
        // 服务器访问地址及端口,例如 http://ip:port
        String serviceUrl = "http://124.70.28.147:8089";
        // 服务名,例如 /u8cloud/api/cmp/hj/insert
        String serviceName = "/u8cloud/api/arap/yf/pagequery";
        // 各服务的参数设置,具体可参照服务描述页面的请求参数示例
        String json = "{\"dwbm\":\"302\"}";

        // 使用U8cloud系统中设置，具体节点路径为：
        // 应用集成 - 系统集成平台 - 系统信息设置
        // 设置信息中具体属性的对照关系如下：
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("trantype", "code"); // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
        map.put("system", "001"); // 系统编码
        map.put("usercode", "15253172037"); // 用户
        map.put("password", "82a21ba38d518b2ee8dc3904412ef3e0"); // 密码，需要 MD5 加密后录入

        // 返回结果
        String result = operator(serviceUrl + serviceName, map, json);
        System.out.println(result);
    }

    private static String operator(String url, Map<String, Object> headparams,String json) {
        HttpClient httpClient = new HttpClient();
        PostMethod httpPost = new PostMethod(url);
        httpPost.setRequestHeader("content-type",
                "application/json;charset=utf-8");
        for (Entry<String, Object> entry : headparams.entrySet()) {
            httpPost.setRequestHeader(entry.getKey(), entry.getValue()
                    .toString());
        }
        try {
            RequestEntity entity = new StringRequestEntity(json,
                    "application/json", "UTF-8");
            httpPost.setRequestEntity(entity);
            httpClient.executeMethod(httpPost);

            return httpPost.getResponseBodyAsString();

        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
